#include "TaskBarIcon.h"
#include "MainFrame.h"
#include "msndef.h"
#include "MsnMessenger.h"

#include "../resource/xpm/logo_offline.xpm"
#include "../resource/xpm/logo_online.xpm"
#include "../resource/xpm/logo_newmsg.xpm"

int ID_TRAY_MENU_RESTORE = wxNewId();
int ID_TRAY_MENU_SIGNIN = wxNewId();
int ID_TRAY_MENU_SIGNOUT = wxNewId();
int ID_TRAY_MENU_OPEN = wxNewId();
int ID_TRAY_MENU_EXIT = wxNewId();

int ID_TRAY_MENU_STATUS = 10000;

BEGIN_EVENT_TABLE(TaskBarIcon, wxTaskBarIcon)
    EVT_CLOSE(                        TaskBarIcon::OnClose)
    EVT_MENU(ID_TRAY_MENU_SIGNIN,     TaskBarIcon::OnSignIn)
    EVT_MENU(ID_TRAY_MENU_SIGNOUT,    TaskBarIcon::OnSignOut)
    EVT_MENU(ID_TRAY_MENU_OPEN,       TaskBarIcon::OnOpen)
    EVT_MENU(ID_TRAY_MENU_EXIT,       TaskBarIcon::OnExit)

    EVT_MENU_RANGE(ID_TRAY_MENU_STATUS, ID_TRAY_MENU_STATUS + MsnDef::STATUS_COUNT, TaskBarIcon::OnChangeStatus)

    EVT_TASKBAR_LEFT_DCLICK(TaskBarIcon::OnLeftDClick)
END_EVENT_TABLE()

TaskBarIcon::TaskBarIcon(MainFrame* frame):m_MainFrame(frame)
{
    //wxIcon logo(sample_xpm);
	//wxIcon logo(logo_xpm);
    wxIcon logo(logo_offline_xpm);
	//wxIcon logo(_T("tux.gif"), wxBITMAP_TYPE_GIF);
    SetIcon(logo, _T("Tuxitty"));
}

void TaskBarIcon::ShowFrame()
{
	//restore icon
	if(App::GetMainFrame()->IsOnline()){
		wxIcon logo(logo_online_xpm);
		//SetIcon(logo);
		//wxIcon logo(_T("tux.gif"), wxBITMAP_TYPE_GIF);
		SetIcon(logo, _T("Tuxitty"));
	}else{
		wxIcon logo(logo_offline_xpm);
		//SetIcon(logo);
		//wxIcon logo(_T("tux.gif"), wxBITMAP_TYPE_GIF);
		SetIcon(logo, _T("Tuxitty"));
	}

    m_MainFrame->Show(true);
    m_MainFrame->Restore();
    m_MainFrame->Raise();
}

void TaskBarIcon::HideFrame()
{
    m_MainFrame->Show(false);

}

void TaskBarIcon::OnSignIn(wxCommandEvent& )
{
     ShowFrame();
}

void TaskBarIcon::OnOpen(wxCommandEvent&)
{
    ShowFrame();
}

void TaskBarIcon::OnClose(wxCloseEvent&)
{
    HideFrame();
}

void TaskBarIcon::OnExit(wxCommandEvent& )
{
    m_MainFrame->Destroy();
}

void TaskBarIcon::OnSignOut(wxCommandEvent& )
{
    //App::GetMainFrame()->GetMessenger()->SignOut();
}


void TaskBarIcon::SwitchIconForNewMessage()
{
	newMsg = !newMsg;

	if(newMsg){
		wxIcon logo(logo_newmsg_xpm);
		//SetIcon(logo);
		//wxIcon logo(_T("msg.gif"), wxBITMAP_TYPE_GIF);
		SetIcon(logo, _T("Tuxitty"));
	}else{
		wxIcon logo(logo_online_xpm);
		//SetIcon(logo);
		//wxIcon logo(_T("tux.gif"), wxBITMAP_TYPE_GIF);
		SetIcon(logo, _T("Tuxitty"));
	}
}

wxMenu* TaskBarIcon::CreatePopupMenu()
{
    wxMenu *menu = new wxMenu;

/*
    wxMenu *statusMenu = new wxMenu;


	for(unsigned int i = 0; i<MsnDef::STATUS_COUNT; ++i){
        statusMenu->AppendRadioItem(ID_TRAY_MENU_STATUS + i, MsnDef::STATUS_DESC_ARRAY[i]);
        if(MsnDef::STATUS_CODE_ARRAY[i].IsSameAs(App::GetMainFrame()->GetCurrentMessenger()->GetUserStatus()))
            statusMenu->Check(ID_TRAY_MENU_STATUS + i, true);
    }


    menu->Append(ID_TRAY_MENU_STATUS, _("My Status"), statusMenu);

    menu->Append(ID_TRAY_MENU_SIGNIN, _T("Sign In"));
    menu->Append(ID_TRAY_MENU_SIGNOUT, _T("Sign Out"));

    if(App::GetMainFrame()->GetCurrentMessenger()->IsAlive()){
        menu->Enable(ID_TRAY_MENU_SIGNIN, false);
    }else{
        menu->Enable(ID_TRAY_MENU_STATUS, false);
        menu->Enable(ID_TRAY_MENU_SIGNOUT, false);
    }
*/

#ifndef __WXMAC_OSX__ /*Mac has built-in quit menu*/
    //menu->AppendSeparator();
    menu->Append(ID_TRAY_MENU_OPEN,  _("Open Tuxitty"));
    menu->Append(ID_TRAY_MENU_EXIT, _("Exit"));
#endif

    return menu;
}

void TaskBarIcon::OnChangeStatus(wxCommandEvent& event)
{
    int index = event.GetId() - ID_TRAY_MENU_STATUS;
    App::GetMainFrame()->GetCurrentMessenger()->ChangeStatus(MsnDef::STATUS_CODE_ARRAY[index]);
}

void TaskBarIcon::OnLeftDClick(wxTaskBarIconEvent&)
{
    ShowFrame();
}
